﻿using ModbusTCP;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AgvDemo
{
    public class ModbusBoxClass
    {
        string ip = string.Empty;
        public Socket LampClient;
        bool isConnectSure = false;
        IPEndPoint LampIpEndPoint;
        public Ping pingAgv = new Ping();

        public string AllowState = "00";
        public ModbusBoxClass(string ipTemp)
        {
            ip = ipTemp;

          
            PlcThread();
        }


        public void PlcThread()
        {
            Thread th1 = new Thread(new ThreadStart(ChargeThreadMethod9));
            th1.IsBackground = true;
            th1.Start();

            Thread th2 = new Thread(new ThreadStart(ReceiveMsg));
            th2.IsBackground = true;
            th2.Start();

        }

        public void ReceiveMsg()
        {
            while (true)
            {
                try
                {
                    if (isConnectSure)
                    {
                        byte[] data = new byte[1024];
                        LampClient.Receive(data);

                        AllowState = data[10].ToString("x2").ToUpper();//把数组转换成16进制字符串

                        Thread.Sleep(100);
                    }
                 
                }
                catch { }
              
            }
        }

        private void ChargeThreadMethod9()
        {
            LampIpEndPoint = new IPEndPoint(IPAddress.Parse(ip), 8080);
            LampClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            while (true)
            {
                try
                {
                    if (pingAgv.Send(ip).Status == IPStatus.Success)
                    {

                        if (!isConnectSure)
                        {
                            try
                            {

                                LampClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                                LampClient.Connect(LampIpEndPoint);
                                isConnectSure = true;




                                MainForm.mainForm.toolStripLabel4.Text = "呼叫盒连接成功";
                                MainForm.mainForm.toolStripLabel4.ForeColor = Color.Lime;

                            }
                            catch
                            {
                                isConnectSure = false;
                                //MainForm.mainForm.toolStripLabel1.Text = "车门机构连接异常";
                                //MainForm.mainForm.toolStripLabel1.ForeColor = Color.Red;

                                MainForm.mainForm.toolStripLabel4.Text = "呼叫盒连接异常";
                                MainForm.mainForm.toolStripLabel4.ForeColor = Color.Red;

                            }

                        }
                        else
                        {
                            try
                            { ReadState(); }
                            catch
                            { }

                        }
                    }
                    else
                    {
                        if (LampClient != null)// && _clientSock.Connected 
                        {
                            isConnectSure = false;
                            try
                            {
                                LampClient.Shutdown(SocketShutdown.Both);
                            }
                            catch (SocketException ex)
                            {
                                //套接字异常
                            }
                            catch (ObjectDisposedException ex)
                            {
                                //已释放异常
                            }

                            try
                            {
                                LampClient.Close();
                            }
                            catch
                            { }


                        }
                        else
                            isConnectSure = false;


                    }


                }
                catch
                {


                    if (LampClient != null)// && _clientSock.Connected 
                    {

                        try
                        {
                            LampClient.Shutdown(SocketShutdown.Both);
                        }
                        catch (SocketException ex)
                        {
                            //套接字异常
                        }
                        catch (ObjectDisposedException ex)
                        {
                            //已释放异常
                        }

                        try
                        {
                            LampClient.Close();
                        }
                        catch
                        { }

                        isConnectSure = false;

                    }
                    else
                        isConnectSure = false;


                }

                Thread.Sleep(1000);
            }
        }

        public void SendMie()
        {
            try
            {
                byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x01, 0x00, 0x02 };
                LampClient.Send(data);
            }
            catch
            { }
        
        }


        public void ReadState()
        {           
            try
            {
                byte[] data = new byte[] { 0x01, 0x02, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01 };
                LampClient.Send(data);
            }
            catch
            { }
        }
    }
}
